package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.CheerThumb;
import com.hpjpw.ido.service.ICheerMomentService;
import com.hpjpw.ido.service.ICheerThumbService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ido/app/cheerThumb")
public class IDoCheerThumbController extends BaseController {
    @Autowired
    ICheerThumbService cheerThumbService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ICheerMomentService cheerMomentService;

    /**
     * 查询加油站消息点赞列表
     */
    /*@ApiOperation("查询加油站消息点赞列表，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list")
    public TableDataInfo list(CheerThumb cheerThumb){
        startPage();
        List<CheerThumb> list = cheerThumbService.selectCheerThumbList(cheerThumb);
        return getDataTable(list);
    }*/

    /**
     * 新增加油站动态的点赞
     */
    @ApiOperation("新增加油站动态的点赞，接收momentId(动态id)")
    @PostMapping()
    public AjaxResult addThumb(@RequestBody CheerThumb cheerThumb) {
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(cheerMomentService.selectCheerMomentById(cheerThumb.getMomentId()))){
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，已点赞，仅根据momentId、thumbedId查询 */
        Long userId = SecurityUtils.getUserId();
        CheerThumb thumbCheck = new CheerThumb();
        thumbCheck.setMomentId(cheerThumb.getMomentId());
        thumbCheck.setThumbedId(userId);
        if (cheerThumbService.selectCheerThumbList(thumbCheck).size()!=0){
            return AjaxResult.error("已点赞");
        }
        /* 安全校验3，无点赞者存在 */
        CheerThumb cheerThumbAdd = new CheerThumb();
        cheerThumbAdd.setMomentId(cheerThumb.getMomentId());
        cheerThumbAdd.setThumbedId(userId);
        cheerThumbAdd.setThumbedName(sysUserService.selectUserById(userId).getNickName());
        cheerThumbAdd.setTime(new Date());
        cheerThumbService.insertCheerThumb(cheerThumbAdd);
        return AjaxResult.success();
    }

    /**
     * 删除加油站动态的点赞
     */
    @ApiOperation("删除加油站动态的点赞，接收momentId(动态id)")
    @DeleteMapping()
    public AjaxResult deleteThumb(@RequestBody CheerThumb cheerThumb) {
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(cheerMomentService.selectCheerMomentById(cheerThumb.getMomentId()))){
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，取消他人点赞，仅根据momentId、userId查询 */
        Long userId = SecurityUtils.getUserId();
        CheerThumb thumbCheck = new CheerThumb();
        thumbCheck.setMomentId(cheerThumb.getMomentId());
        thumbCheck.setThumbedId(userId);
        List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(thumbCheck);
        if (StringUtils.isNull(cheerThumbList)||cheerThumbList.size()==0){
            return AjaxResult.error("未点赞");
        }
        cheerThumbService.deleteCheerThumbById(cheerThumbList.get(0).getId());
        return AjaxResult.success();
    }
}
